let utils = require('../../../utils/utils.js')
Page({

    /**
     * Page initial data
     */
    data: {
        sceneId: '', //场景id
        status: 0, //是否拥有
        scene: '',
    },

    /**
     * Lifecycle function--Called when page load
     */
    onLoad(options) {
        let _this = this;
        let sceneId = options.id;
        let status = options.status;
        _this.setData({
            sceneId: sceneId,
            status: status,
        });
        utils.querySceneById(sceneId).then(res => {
            _this.setData({
                scene: res.data.data
            });
        }).catch(error => {
            console.error(error);
        });
    },

    /**
     * Lifecycle function--Called when page is initially rendered
     */
    onReady() {

    },

    /**
     * Lifecycle function--Called when page show
     */
    onShow() {

    },

    /**
     * Lifecycle function--Called when page hide
     */
    onHide() {

    },

    /**
     * Lifecycle function--Called when page unload
     */
    onUnload() {

    },

    /**
     * Page event handler function--Called when user drop down
     */
    onPullDownRefresh() {

    },

    /**
     * Called when page reach bottom
     */
    onReachBottom() {

    },

    /**
     * Called when user click on the top right corner to share
     */
    onShareAppMessage() {

    },

    pay() {
        let _this = this;
        let userInfo = wx.getStorageSync("userInfo");
        wx.request({
            url: 'https://xinai.icu/api/toy/buyScene',
            method: 'POST',
            header: {
                'Content-Type': 'application/json',
                'token': userInfo.token,
            },
            data: {
                sceneId: _this.data.sceneId,
            },
            success(res) {
                console.log("购买成功：", res)
                utils.toast("购买成功");
                //跳转到首页
                utils.getCallBackUrl().catch(() => {
                    wx.redirectTo({
                        url: '/pages/index/index'
                    })
                });
            },
            fail(err) {
                utils.toast("购买失败：", err);
                console.log(err)
            }
        });
    }
})